package com.example.musicbox.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.musicbox.config.MyException;
import com.example.musicbox.entity.MMessage;
import com.example.musicbox.entity.MUser;
import com.example.musicbox.enums.AboutMusic;
import com.example.musicbox.enums.LogDoType;
import com.example.musicbox.mapper.MMessageMapper;
import com.example.musicbox.mapper.MUserMapper;
import com.example.musicbox.service.MMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.musicbox.utils.Result;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 帅哥是我
 * @since 2022-08-01
 */
@Service
public class MMessageServiceImpl extends ServiceImpl<MMessageMapper, MMessage> implements MMessageService {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private MMessageMapper mMessageMapper;

    @Resource
    private MUserMapper mUserMapper;

    @Resource
    private MLogServiceImpl mLogService;

    /**
     * 用户上传音乐
     * @param mMessage
     * @param token
     * @return
     */
    @Override
    public Result insertMessage(MMessage mMessage, String token) {
        try {
            Integer userId = (Integer) redisTemplate.opsForValue().get(token);
            Integer adminId = mUserMapper.findAdmin();
            MUser mUser = mUserMapper.selectById(userId);
            mMessage.setSenduserid(userId);
            //当发送方为超级管理员时，发送方姓名直接改为超级管理员
            if (userId.equals(adminId)) {
                mMessage.setSendusername("系统管理员");
            } else {
                mMessage.setSendusername(mUser.getUsername());
            }
            //这代表是用户上传歌曲
            if (mMessage.getSendtype().equals("0")) {
                //用户上传音乐时，默认管理员为接收方
                mMessage.setAcceptuserid(adminId);
                mMessage.setDetailinfo("用户上传了一首音乐");
            }
            mMessageMapper.insert(mMessage);
        } catch (Exception e) {
            throw new MyException("信息发送失败");
        }
        return Result.ok("发送成功");
    }


    /**
     * 单纯一个构造
     * @param senduserid
     * @param acceptuserid
     * @param sendtype
     * @param detailinfo
     * @param sendusername
     * @param musicId
     * @return
     */
    public Result sendMessage(Integer senduserid,Integer acceptuserid,String sendtype,
                              String detailinfo,String sendusername,Integer musicId,String token)

    {
        MMessage build = MMessage.builder()
                .senduserid(senduserid)
                .acceptuserid(acceptuserid)
                .sendtype(sendtype)
                .detailinfo(detailinfo)
                .sendusername(sendusername)
                .musicId(musicId)
                .build();
        return insertMessage(build,token);
    }


    /**
     * 用户删除消息
     * @param id
     * @param token
     * @return
     */
    @Override
    public Result deleteMessage(Integer id, String token) {
        try {
            Integer userId = (Integer) redisTemplate.opsForValue().get(token);
            MMessage mMessage = mMessageMapper.selectById(id);
            //在这里我只让用户删除自己的收信箱
            if(mMessage.getAcceptuserid().equals(userId)) {
                mMessageMapper.deleteById(id);
                mLogService.insertLog("用户删除收信箱", AboutMusic.NOTHINGABOUTMUSIC,null, LogDoType.DELETE,token);
                return Result.ok("信息删除成功");
            }else {
                return Result.error("用户没有相关权限");
            }
        }catch (Exception e){
            throw new MyException("消息删除失败");
        }
    }

    @Override
    public Result selectById(Integer id, String token) {
        try {
            MMessage mMessage = mMessageMapper.selectById(id);
            //查看之后，将其标记为已读
            mMessage.setMstatus("1");
            mMessageMapper.updateById(mMessage);
            mLogService.insertLog("用户查看消息",AboutMusic.NOTHINGABOUTMUSIC,null, LogDoType.CHECK,token);
            return Result.ok("查看信息成功").put("mMessage",mMessage);
        }catch (Exception e){
            throw new MyException("用户查看消息失败");
        }
    }

    @Override
    public Result selectByList(String token,Integer p,Integer s) {
        try {
            Integer userId = (Integer) redisTemplate.opsForValue().get(token);
            QueryWrapper<MMessage> wrapper = new QueryWrapper<>();
            wrapper.eq("acceptUserId",userId);
            Page<MMessage> page = mMessageMapper.selectPage(new Page<>(p, s), wrapper);
            return Result.ok("查询成功").put("page",page);
        }catch (Exception e){
            throw new MyException("查询失败");
        }
    }
}
